package com.letv.whatslive.model.convert;

import com.letv.whatslive.model.VideoUploadInfo;
import com.letv.whatslive.model.utils.ObjUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * Created by haojiayao on 16-3-29.
 */
public class VideoUploadInfoConvert {

    public static DBObject castVideoUploadInfoToDBObject(VideoUploadInfo videoUploadInfo) {

        DBObject dbo = new BasicDBObject();

        dbo.put("outKey", ObjUtils.toLong(videoUploadInfo.getOutKey()));
        dbo.put("mid", ObjUtils.toLong(videoUploadInfo.getMid()));
        dbo.put("vid", ObjUtils.toLong(videoUploadInfo.getVid()));
        dbo.put("duration", ObjUtils.toLong(videoUploadInfo.getDuration(), 0L));
        if (videoUploadInfo.getStatus() != null) {

            dbo.put("status", ObjUtils.toInteger(videoUploadInfo.getStatus()));
        }
        dbo.put("shortVideoMid", ObjUtils.toLong(videoUploadInfo.getShortVideoMid()));
        dbo.put("shortVideoVid", ObjUtils.toLong(videoUploadInfo.getShortVideoVid()));
        dbo.put("shortVideoDuration", ObjUtils.toLong(videoUploadInfo.getShortVideoDuration(), 0L));
        if (videoUploadInfo.getShortVideoStatus() != null) {

            dbo.put("shortVideoStatus", ObjUtils.toInteger(videoUploadInfo.getShortVideoStatus()));
        }
        dbo.put("createTime", ObjUtils.toLong(videoUploadInfo.getCreateTime(), System.currentTimeMillis()));
        dbo.put("updateTime", System.currentTimeMillis());

        return dbo;
    }

    public static VideoUploadInfo castDBObjectToVideoUploadInfo(DBObject dbObject) {

        if (dbObject != null) {

            VideoUploadInfo videoUploadInfo = new VideoUploadInfo();
            videoUploadInfo.setId(ObjUtils.toLong(dbObject.get("_id")));
            videoUploadInfo.setOutKey(ObjUtils.toLong(dbObject.get("outKey")));
            videoUploadInfo.setMid(ObjUtils.toLong(dbObject.get("mid")));
            videoUploadInfo.setVid(ObjUtils.toLong(dbObject.get("vid")));
            videoUploadInfo.setDuration(ObjUtils.toLong(dbObject.get("duration"), 0L));
            videoUploadInfo.setStatus(ObjUtils.toInteger(dbObject.get("status")));
            videoUploadInfo.setShortVideoMid(ObjUtils.toLong(dbObject.get("shortVideoMid")));
            videoUploadInfo.setShortVideoVid(ObjUtils.toLong(dbObject.get("shortVideoVid")));
            videoUploadInfo.setShortVideoDuration(ObjUtils.toLong(dbObject.get("shortVideoDuration"), 0L));
            videoUploadInfo.setShortVideoStatus(ObjUtils.toInteger(dbObject.get("shortVideoStatus")));
            videoUploadInfo.setCreateTime(ObjUtils.toLong(dbObject.get("createTime")));
            videoUploadInfo.setUpdateTime(ObjUtils.toLong(dbObject.get("updateTime")));

            return videoUploadInfo;
        } else {
            return null;
        }
    }
}
